package Core;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.Random;

public class Helper {
    public static BinaryWebSocketFrame assembleBinaryWebFrame(int id, byte[] content) {
        final int HEADSIZE = 6;

        byte[] result = new byte[content.length + HEADSIZE];

        // size
        result[0] = (byte) ((content.length & 65280) >> 8);
        result[1] = (byte) ((content.length & 255));

        // id
        result[2] = (byte) ((id & 65280) >> 8);
        result[3] = (byte) ((id & 255));

        // reserved
        result[4] = 0;
        result[5] = 0;

        System.arraycopy(content, 0, result, HEADSIZE, content.length);
        BinaryWebSocketFrame bwsf = new BinaryWebSocketFrame(Unpooled.copiedBuffer(result));
        return bwsf;
    }

    public static int RandomRangeInt(int min, int max) {
            Random rand = new Random();
            int seed = rand.nextInt();
            rand = new Random(seed);
            int randomNum = rand.nextInt((max - min) + 1) + min;
            return randomNum;
    }
}
